package com.example.controller;

import com.example.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping(value = "/test/")
public class HelloWorld {

    @RequestMapping("/test")
    public String helloWorld() {
        return "test";
    }

    @RequestMapping("/qrcode")
    @ResponseBody
    public String qrcode() {
        QRCodeUtil.zxingCodeCreate("1212", "./", 250, "");
        return "qrcode";
    }

    @RequestMapping("/barcode")
    @ResponseBody
    public String barcode() {
        QRCodeUtil.barCode("121212", "./", 100);
        return "barcode";
    }

    @PostMapping(value = "/files/data/upload/batch")
//    @ResponseBody
    public String handleFileUpload(HttpServletRequest request, @RequestParam(value = "file", required = true) List<MultipartFile> files) {
        String lotno = request.getParameter("lotno");
        System.out.println("lotno:" + lotno);
        String goodsid = request.getParameter("goodsid");
        System.out.println("goodsid:" + goodsid);
        try {
            StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
//            Iterator<String> files = multipartHttpServletRequest.getFileNames();
//            while (files.hasNext()) {
//                MultipartFile multipartFile = multipartHttpServletRequest.getFile(files.next());
//
//                OutputStream out = new FileOutputStream(new File(multipartFile.getOriginalFilename()));
//                BufferedOutputStream bout = new BufferedOutputStream(out);
//                InputStream in = multipartFile.getInputStream();
//                byte[] bys = new byte[1024];
//                int len = 0;
//                while ( (len = in.read(bys)) != -1) {
//                    bout.write(bys, 0, len);
//                }
//                in.close();
//                bout.close();
//                out.close();
//            }

            List<MultipartFile> fileList = multipartHttpServletRequest.getFiles("file");
            for (MultipartFile multipartFile : fileList) {
                OutputStream out = new FileOutputStream(new File(multipartFile.getOriginalFilename()));
                BufferedOutputStream bout = new BufferedOutputStream(out);
                InputStream in = multipartFile.getInputStream();
                byte[] bys = new byte[1024];
                int len = 0;
                while ( (len = in.read(bys)) != -1) {
                    bout.write(bys, 0, len);
                }
                in.close();
                bout.close();
                out.close();
            }

        } catch (Exception e) {
            return e.getMessage();
        }
        return "{\"code\":\"00000\",\"message\":\"成功\",\"data\":[\"image1815459487795556322.png\",\"image18154594877955561212.png\"]}";
    }

    @RequestMapping(value = "/test/post", method = RequestMethod.POST)
    @ResponseBody
    public String testPost(HttpServletRequest request) {
        String name = request.getParameter("name");
        return "success";
    }

    @RequestMapping("/home")
    public String home() {
        return "home";
    }

    @GetMapping("/downloadTest")
    @ResponseBody
    public void downloadLocal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = request.getParameter("fileName");
        /** 获取静态文件的路径 .*/
        //String path = ResourceUtils.getURL("classpath:").getPath() + "static/file/" + fileName;
        String path = "F:\\IdeaProjects\\demo\\" + fileName;
        /** 获取文件的名称 . */
        fileName = path.substring(path.lastIndexOf("/") +1);
        File file = new File(path);
        if (!file.exists()){
//            logger.error("路径有误，文件不存在！");
            System.out.println("路径有误，文件不存在！");
        }

        /** 将文件名称进行编码 */
        response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
        response.setContentLengthLong(file.length());
        response.setContentType("content-type:octet-stream");

        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        OutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1){ /** 将流中内容写出去 .*/
            outputStream.write(buffer ,0 , len);
        }
        inputStream.close();
        outputStream.close();
    }

}
